package cn.engage.pet.service.impl;


import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.basic.util.BaiduAuditUtils;
import cn.engage.pet.domain.Pet;
import cn.engage.pet.domain.PetOnlineAuditLog;
import cn.engage.pet.mapper.PetMapper;
import cn.engage.pet.mapper.PetOnlineAuditLogMapper;
import cn.engage.pet.service.PetDetailService;
import cn.engage.pet.service.PetService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {

    @Autowired
    private PetDetailService petDetailService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        pet.getDetail().setPetId(pet.getId());
        petDetailService.add(pet.getDetail());
    }

    /* 宠物下架 */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.batchOffsale(ids);
    }
    /* 宠物上架 */
    @Override
    public void onsale(List<Long> ids) {
        // 筛选出下架状态的商品上架
        Map<Long, Pet> map = petMapper.findAll().stream().filter(pet -> pet.getState() == 0).collect(Collectors.toMap(Pet::getId, e -> e));
        // 遍历id去map里去对应信息
        StringBuilder stringbuilder = new StringBuilder();
        Date date = new Date();
        for (Long id : ids) {
            Pet pet = map.get(id);
            // 不为空则表示要上架，进行参数校验
            if (pet != null){
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    stringbuilder.append("宠物上架百度AI审核名称失败，宠物名称为：").append(pet.getName()).append(";");
                }
                if (pet.getResources().split(",").length > 1){
                    // 有多张图片，图片用逗号隔开的，循环遍历
                    for (String s : pet.getResources().split(",")) {
                        if (!BaiduAuditUtils.ImgCensor(s)){
                            stringbuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：").append(pet.getResources()).append(";");
                        }
                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())){
                        stringbuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：").append(pet.getResources()).append(";");
                    }
                }
                // 表示有图片或者名字不合格
                if (stringbuilder.length() > 0){
                   PetOnlineAuditLog petOnlineAuditLog = PetOnlineAuditLog.builder()
                            .auditTime(date)
                            .petId(id)
                            .state(0)
                            .note(stringbuilder.toString())
                            .build();
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                petMapper.update(pet);
            }
            }
        }

    @Override
    public Pet findPetDetail(Long id) {
        return petMapper.findPetDetail(id);
    }
}

